package com.example.project10;

import android.app.Application;
import android.content.Context;
import android.content.Intent;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.util.ArrayList;
import java.util.List;

public class SharedViewModel extends AndroidViewModel {
    // 使用 MutableLiveData 来存储可变数据
    private final MutableLiveData<String> userEmail = new MutableLiveData<>();
    private final MutableLiveData<String> currentInterest = new MutableLiveData<>("未选择");
    private final MutableLiveData<Boolean> logoutEvent = new MutableLiveData<>();
    
    // 推荐列表数据
    private final MutableLiveData<List<RecommendationItem>> recommendationList = new MutableLiveData<>();
    private final MutableLiveData<Boolean> isRefreshing = new MutableLiveData<>(false);
    
    public SharedViewModel(Application application) {
        super(application);
    }

    // 为外部提供不可变的 LiveData，防止数据被意外修改
    public LiveData<String> getUserEmail() {
        return userEmail;
    }

    public LiveData<String> getCurrentInterest() {
        return currentInterest;
    }

    public LiveData<Boolean> getLogoutEvent() {
        return logoutEvent;
    }

    // 设置用户邮箱
    public void setUserEmail(String email) {
        userEmail.setValue(email);
    }

    // 设置当前兴趣
    public void setCurrentInterest(String interest) {
        currentInterest.setValue(interest);
    }

    // 退出登录
    public void logout() {
        // 清除token和自动登录状态
        SharedPreferencesUtil.clearToken(getApplication());
        
        // 通知观察者退出登录事件
        logoutEvent.setValue(true);
    }

    // 重置退出登录事件状态
    public void onLogoutEventHandled() {
        logoutEvent.setValue(false);
    }

    // 推荐列表相关方法
    public LiveData<List<RecommendationItem>> getRecommendationList() {
        return recommendationList;
    }

    public LiveData<Boolean> getIsRefreshing() {
        return isRefreshing;
    }

    public void loadRecommendations() {
        isRefreshing.setValue(true);
        
        // 模拟网络请求
        new Thread(() -> {
            try {
                Thread.sleep(2000); // 模拟网络延迟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            List<RecommendationItem> items = new ArrayList<>();
            items.add(new RecommendationItem(1, "Java核心技术", "深入理解Java编程语言的核心概念和高级特性", "编程", "", 4.8));
            items.add(new RecommendationItem(2, "古典音乐欣赏", "带你走进古典音乐的殿堂，感受音乐之美", "音乐", "", 4.6));
            items.add(new RecommendationItem(3, "背包客指南", "为自助旅行者提供的实用攻略和经验分享", "旅行", "", 4.7));
            items.add(new RecommendationItem(4, "家常菜谱大全", "简单易学的家常菜做法，让你轻松成为烹饪高手", "美食", "", 4.5));
            items.add(new RecommendationItem(5, "健身训练计划", "科学有效的健身方法和训练计划", "健身", "", 4.9));

            recommendationList.postValue(items);
            isRefreshing.postValue(false);
        }).start();
    }
    
    // 刷新推荐列表
    public void refreshRecommendations() {
        loadRecommendations();
    }
    
    // 删除推荐项
    public void removeRecommendationItem(int position) {
        List<RecommendationItem> currentList = recommendationList.getValue();
        if (currentList != null && position >= 0 && position < currentList.size()) {
            List<RecommendationItem> newList = new ArrayList<>(currentList);
            newList.remove(position);
            recommendationList.setValue(newList);
        }
    }
}